<?php

/**
 * @category   Ac
 * @package    Application_Model
 * @copyright  Orient Network Ltd
 * @license    Closed
 * @author     SARDS
 */
class Model_Building extends Ac_Model {

    protected static $_collection = 'building';
    protected static $_transcript = array(
        'r_id' => 'Reference_ID',
        'l_date' => 'Last_Postdate',
        'url_eng' => 'Url',
        'dist_eng' => 'District',
        'n_eng' => 'Name',
        'es_eng' => 'Estate',
        'st_eng' => 'Story',
        'ba_eng' => 'Basement',
        'u_eng' => 'Unit',
        'yr_eng' => 'Year',
        'snum_eng' => 'Street_no',
        'sname_eng' => 'Street_name',
        'a_eng' => 'Area',
        'r_org_eng' => 'Resident_org',
        'url_cn' => 'UrlCn',
        'dist_cn' => 'DistrictCn',
        'n_cn' => 'NameCn',
        'es_cn' => 'EstateCn',
        'st_cn' => 'StoryCn',
        'ba_cn' => 'BasementCn',
        'u_cn' => 'UnitCn',
        'yr_cn' => 'YearCn',
        'snum_cn' => 'Street_noCn',
        'sname_cn' => 'Street_nameCn',
        'a_cn' => 'AreaCn',
        'r_org_cn' => 'Resident_orgCn'
    );

    public function __construct($data = array(), $config = array()) {
        $config['collection'] = 'buildings';
        parent::__construct($data, $config);
    }

    public static function all(array $query = array()) {
        $q = array();
        $ts = static::$_transcript;
        $ts = array_flip($ts);
        foreach ($query as $k => $v) {
            if (isset($ts[$k])) {
                $q[$ts[$k]] = $v;
            } else {
                $q[$k] = $v;
            }
        }


        //from Shanty
        $query = $q;
        $inheritance = static::getCollectionInheritance();
        if (count($inheritance) > 1) {
            $query['_type'] = $inheritance[1]; //get the top and not the custom
        }
        $cursor = static::getMongoCollection(false)->find($query);

        $config = array();
        $config['connectionGroup'] = static::getConnectionGroupName();
        $config['db'] = static::getDbName();
        $config['collection'] = static::getCollectionName();
        $config['documentClass'] = static::getDocumentClass();
        $config['documentSetClass'] = static::getDocumentSetClass();

        return new Shanty_Mongo_Iterator_Cursor($cursor, $config);
    }

}